using CommunityToolkit.Mvvm.ComponentModel;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using NuGetManager.CommunityToolkit.NuGet;



namespace PackageManager.ViewModels
{
    public partial class NugetPackageSearchItem : ObservableObject
    {
        public NugetPackageSearchItem() { }
        
       
        public NugetPackageSearchItem(IPackageSearchMetadata searchItem)
        {
            _identity = searchItem?.Identity;
            id = _identity?.Id;
            title = searchItem?.Title;
            installedVersion = "";
            // Find installed version !
            canInstall = true;
            selectedVersion = installedVersion;
            licenseUrl = searchItem?.LicenseUrl?.AbsoluteUri;
            projectUrl = searchItem?.ProjectUrl?.AbsoluteUri;
            publishTime = searchItem?.Published?.ToString();
            tags = searchItem?.Tags;
            description = searchItem?.Description;
            authors = searchItem?.Authors;
            dependencies = searchItem?.DependencySets?.ToList();
        }
        
        public NugetPackageSearchItem(PackageIdentity searchItem)
        {
            _identity =searchItem;
            id = _identity.Id;
            nuGetVersion = _identity.Version;
        }
        
     
        [ObservableProperty] private  PackageIdentity _identity = null!;
        
        [ObservableProperty] private string? id ;
        
        [ObservableProperty] private string? authors ;
        
        [ObservableProperty] private string? description;
        
        [ObservableProperty] private string? iconUrl ;

        [ObservableProperty] public string? title;
        
        [ObservableProperty] private bool? isInstalled;
        
        [ObservableProperty] private bool? canInstall;
        
        [ObservableProperty] private NuGetVersion? nuGetVersion;
        
        [ObservableProperty] private string? installedVersion = null!;
        
        [ObservableProperty] private string licenseUrl = null!;
        
        [ObservableProperty] private string projectUrl = null!;
        
        [ObservableProperty] private string? publishTime;
        
        [ObservableProperty] private string tags = null!;
        
        [ObservableProperty] private bool requireLicenseAcceptance;
        
        [ObservableProperty] private string selectedVersion = null!;
        
        [ObservableProperty] private List<PackageDependencyGroup> dependencies = null!;
        
        [ObservableProperty] private System.Collections.ObjectModel.ObservableCollection<string> versionList = null!;

        public override string ToString()
        {
            if (String.IsNullOrEmpty(installedVersion))
            {
                return id;
            }

            return id + "@" + installedVersion;
        }
    }
}
